package com.black.app.service.impl.find;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.black.app.domain.find.AppFindZhangzhishi;
import com.black.app.domain.req.find.add.Zhangzhishiaddreq;
import com.black.app.domain.req.find.edit.ZhangzhishiEssayEidtreq;
import com.black.app.domain.req.find.query.AppFindZhangzhishiessayreq;
import com.black.app.domain.resp.find.FindZhangzhishiresp;
import com.black.app.mapper.find.AppFindZhangzhishiMapper;
import com.black.app.service.find.AppFindZhangzhishiService;
import com.black.common.exception.ServiceException;
import com.black.common.exception.database.PrimaryException;
import com.black.common.exception.database.RecordNotFoundException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.black.common.constant.CacheConstants;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
* @author 30223
* @description 针对表【app_find_zhangzhishi(涨知识-文章)】的数据库操作Service实现
* @createDate 2024-10-25 11:59:26
*/
@Service
public class AppFindZhangzhishiServiceImpl extends ServiceImpl<AppFindZhangzhishiMapper, AppFindZhangzhishi>
    implements AppFindZhangzhishiService {

    @Autowired
    private RedisTemplate redisTemplate;



    @Override
    public boolean add(Zhangzhishiaddreq req) {
        if(this.baseMapper.exists(new LambdaQueryWrapper<AppFindZhangzhishi>().eq(AppFindZhangzhishi::getTitle,req.getTitle()))) {
            throw new ServiceException("文章名称重复");
        }

        AppFindZhangzhishi entity = new AppFindZhangzhishi();
        BeanUtils.copyProperties(req, entity);
        boolean save = this.save(entity);
        if (save){
            redisTemplate.opsForValue().set(CacheConstants.APP_FIND_ZHANGZHISHI_KEY+req.getTitle(),entity);
        }
        return save;
    }

    @Override
    public PageInfo<AppFindZhangzhishi> paginationQuery(AppFindZhangzhishiessayreq req) {
        return PageHelper.startPage(req.getPageNum(), req.getPageSize()).doSelectPageInfo(() -> {
            this.baseMapper.paginationQuery(req);
        });
    }


    @Override
    public List<AppFindZhangzhishi> findByTitleId(Long typeId) {
        return this.baseMapper.findByTitleId(typeId);
    }

    @Override
    public FindZhangzhishiresp findById(Long id) {
        if (Objects.isNull(id)) {
            throw new PrimaryException();
        }

        AppFindZhangzhishi FindZhangzhishi = this.baseMapper.getid(id);

        if (Objects.isNull(FindZhangzhishi)) {
            throw new RecordNotFoundException();
        }

        FindZhangzhishiresp resp = new FindZhangzhishiresp();
        BeanUtils.copyProperties(FindZhangzhishi, resp);

        return resp;
    }

    @Override
    public boolean deleteById(Long id) {

        if (Objects.isNull(id)) {
            throw new PrimaryException();
        }

        AppFindZhangzhishi entity = this.getById(id);

        if (Objects.isNull(entity)) {
            throw new RecordNotFoundException();
        }

        return this.removeById(id);
//        final AppFindZhangzhishiServiceImpl appFindZhangzhishiService = this;
//
//        if (Objects.isNull(ids) || ids.length == 0) {
//            throw new PrimaryException();
//        }
//
//        this.redisTemplate.execute(new SessionCallback<Void>() {
//
//            @Override
//            public Void execute(RedisOperations operations) throws DataAccessException {
//                //开启redis事务
//                operations.multi();
//
//                // 循环删除
//                try {
//                    for (Long id : ids) {
//                        AppFindZhangzhishi entity = appFindZhangzhishiService.baseMapper.getById(id);
//                        if (Objects.nonNull(entity)) {
//                            //删除缓存
//                            operations.delete(CacheConstants.APP_FIND_ZHANGZHISHI_KEY + entity.getTitle());
//                            //删除数据库
//                            appFindZhangzhishiService.removeById(id);
//                        }
//                    }
//                    // 提交redis事务
//                    operations.exec();
//                }catch (Exception e){
//                    operations.discard();
//                    throw e;
//                }
//                return null;
//            }
//        });



    }

    @Override
    public boolean updateessay(ZhangzhishiEssayEidtreq req) {
        AppFindZhangzhishi entity = this.getById(req.getEssayid());
        if (Objects.nonNull(entity)) {
            entity.setTitle(req.getTitle());
            entity.setBrief(req.getBrief());
            entity.setTitleid(req.getTitleid());
            entity.setImageUrl(req.getImageUrl());
            entity.setDianzhan(req.getDianzhan());
            entity.setComments(req.getComments());
            entity.setUserid(req.getUserid());
            entity.setUseraddress(req.getUseraddress());
            boolean result = this.updateById(entity);
            if (result){
                redisTemplate.opsForValue().set(CacheConstants.APP_FIND_ZHANGZHISHI_KEY+req.getTitle(),entity);
            }
        }
        return true;
    }
}




